﻿//可使用權限    無
//2010-05-20    判斷使用者是否太久未操作
//2010-06-30    取消重登告警機制

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace iEN
{
    public partial class CheckAlive : System.Web.UI.Page
    {
        private string strTimeOut = System.Configuration.ConfigurationManager.AppSettings["TimeOut"];
        private string strTimeOutAlarm = System.Configuration.ConfigurationManager.AppSettings["TimeOutAlarm"];

        #region protected void Page_Load(object sender, EventArgs e)        //頁面初始
        protected void Page_Load(object sender, EventArgs e)
        {
            lbCountDown.Text = "將於" + strTimeOut + "分00秒後自動登出";
            if (Session["LoginName"] != null)
            {
                string strUserName = Session["LoginName"].ToString();
                lblLoginName.Text = strUserName + " 您好！";
            }
        }
        #endregion

        #region protected void timerCheck_Tick(object sender, EventArgs e)
        protected void timerCheck_Tick(object sender, EventArgs e)
        {
            CheckTimeout();         //判斷是否TimeOut
        }
        #endregion

        #region private void CheckTimeout()     //判斷是否TimeOut
        private void CheckTimeout()
        {
            if (strTimeOut != "*")
            {
                int intTimeOut = Convert.ToInt16(strTimeOut);
                int intTimeOutAlarm = Convert.ToInt16(strTimeOutAlarm);

                if (Session["AliveTime"] == null || Session["AliveTime"].ToString () == "")
                {                    
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請先登入！！');parent.location.href='Login.aspx';", true);
                }
                else
                {
                    DateTime dtsSessionTime = Convert.ToDateTime(Session["AliveTime"].ToString());
                    TimeSpan tsDiff = new TimeSpan(DateTime.Now.Ticks - dtsSessionTime.Ticks);
                    //if (tsDiff.TotalMinutes > intTimeOutAlarm && tsDiff.TotalMinutes < intTimeOut)
                    //{
                    //    int iDiff = Convert.ToInt16(intTimeOut - tsDiff.TotalMinutes);
                    //    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('您太久未操作頁面，安全性考量約" + iDiff.ToString() + "分鐘後將導回登入頁面！')", true);
                    //}

                    if (tsDiff.TotalMinutes >= intTimeOut)
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('您太久未操作頁面，安全性考量請重新登入！！');parent.location.href='Login.aspx';", true);
                    }
                }
            }
        }
        #endregion
        #region protected void timerCountDown_Tick(object sender, EventArgs e)//顯示倒數
        protected void TimerCountDown_Tick(object sender, EventArgs e)
        {

            try
            {
                DateTime dtsSessionTime = Convert.ToDateTime(Session["AliveTime"]);
                TimeSpan tsDiff = new TimeSpan(DateTime.Now.Ticks - dtsSessionTime.Ticks);
                Double dbTimeOut = Double.Parse(strTimeOut) * 60;

                if ((dbTimeOut - tsDiff.TotalSeconds) >= 0)
                    lbCountDown.Text = "將於" + Math.Floor((dbTimeOut - tsDiff.TotalSeconds) / 60) + "分" + Math.Floor((dbTimeOut - tsDiff.TotalSeconds) % 60) + "秒後自動登出";
                else
                    lbCountDown.Text = "將於0分0秒後自動登出";
            }
            catch
            {
            }
        }
        #endregion

    }
}
